<?php


namespace App\Transforms;


use App\Enum\DriverEnum;
use BobCoder\Construct\Foundation\Transform\TransformContract;

class OrderTransformer implements TransformContract
{

    public function transform($attributes)
    {
        return $attributes;
    }

    /**
     * 格式化数据
     * @param $list
     * @return mixed
     * @Author 姿势就是力量
     */
    public function transformList($list){
        if (!$list) return $list;
        foreach ($list as &$item) {
            $item['status']=DriverEnum::order($item['status']);
            $item['source']=DriverEnum::source($item['source']);
             //格式化订单路程
            if (isset($item['distance']) && $item['distance']) {
                $item['book_time'] = $item['distance']['book_time'];
                $item['reality_start_time'] = $item['distance']['reality_start_time'];
                $item['reality_start'] = $item['distance']['reality_start'];
                $item['reality_end'] = $item['distance']['reality_end'];
            }
            unset($item['distance']);
            //格式化业务类型
            if (isset($item['service_type']) && $item['service_type']) {
                $item['name'] = $item['service_type'];
            }else{
                $item['name'] = '';
            }
            unset($item['service_type']);
            //格式化公司类型
            if (isset($item['corporation']) && $item['corporation']) {
                $item['copany_name'] = $item['corporation']['name'];
            }
            unset($item['corporation']);
            //格式化用户
            if (isset($item['passenger']) && $item['passenger']) {
                $item['user_name'] = $item['passenger']['name'];
                $item['user_phone'] = $item['passenger']['phone'];
            }
            unset($item['passenger']);
            //格式化司机
            if (isset($item['driver']) && $item['driver']) {
                $item['real_name'] = $item['driver']['real_name'];
                $item['phone'] = $item['driver']['phone'];
            }else{
                $item['real_name'] = '';
                $item['phone'] = '';
            }
            unset($item['driver']);
            //格式化车牌号
            if (isset($item['car']) && $item['car']) {
                $item['number'] = $item['car']['number'];
            }else{
                $item['number'] = '';
            }
            unset($item['car']);
        }

        return $list;
    }
}
